package main.tileMap;


import java.awt.Graphics2D;
import java.awt.Rectangle;




import main.GamePanel;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class Level {
	
	public int width, height;
	
	public Background[][] bg = new Background[width+10][height + 10];
	//public Solid[][] solid = new Solid[width][height];
	//public Item[][] item = new Item[width][height];
	
	public final String Dpath ="res/level_";
	public String path = Dpath;
	
	public TiledMap map = null;
	
	public Level(int id) {
		path = Dpath + Integer.toString(id) + ".tmx";
		System.out.println(path);
		
		try{
			map = new TiledMap(path, false);
		}catch(SlickException e){
			System.out.println("Error Loading Map!");
		}
		width = map.getWidth();
		height = map.getHeight();
		
		for(int x = 0; x<bg.length; x++){
			for(int y = 0; y< bg[0].length; y++){
				bg[x][y] = new Background (new Rectangle(x*Tile.size,y*Tile.size, Tile.size, Tile.size), Tile.blank);
				//solid[x][y] = new Solid (new Rectangle(x*Tile.size, y*Tile.size, Tile.size, Tile.size), Tile.blank);
				//item[x][y] = new Item (new Rectangle(x*Tile.size, y*Tile.size, Tile.size, Tile.size), Tile.blank);
				
			}
		}
		
		loadWorld();
		
	}
	
	public void loadWorld(){
		int background = map.getLayerIndex("background");
		//int solids = map.getLayerIndex("collision");
		//int items = map.getLayerIndex("object");
		
		for(int x = 0; x<bg.length; x++){
			for(int y = 0; y< bg[0].length; y++){
				
				//background
				if(map.getTileId(x,y,background) == 9){
					bg[x][y].id = Tile.sueloMaderaSupIzq;
				}
				if(map.getTileId(x,y,background) == 10){
					bg[x][y].id = Tile.sueloMaderaSupDe;
				}
				if(map.getTileId(x,y,background) == 1){
					bg[x][y].id = Tile.sueloMaderaSup;
				}
				if(map.getTileId(x,y,background) == 4){
					bg[x][y].id = Tile.sueloMaderaInf;
				}
				if(map.getTileId(x,y,background) == 12){
					bg[x][y].id = Tile.sueloMaderaInfDe;
				}
				if(map.getTileId(x,y,background) == 11){
					bg[x][y].id = Tile.sueloMaderaInfIzq;
				}
				if(map.getTileId(x,y,background) == 3){
					bg[x][y].id = Tile.sueloMaderaDe;
				}
				if(map.getTileId(x,y,background) == 2){
					bg[x][y].id = Tile.sueloMaderaIzq;
				}

				
				//solids

				
				/*items
				if(map.getTileId(x,y,items) == 129){
					item[x][y].id = Tile.leaves;
				}
				*/
			}
		}	
		
	}
	
	public void tick(double delta){
		
	}
	
	public void render(Graphics2D g){
		for(int x = 0; x<width; x++){
			for(int y = 0; y<height; y++){
				
					bg[x][y].render(g);
				
			
			}

		}
	}
		
}
